{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 重采样及频率转换\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from datetime import datetime\n",
    "from datetime import timedelta\n",
    "from dateutil.parser import parse\n",
    "from pandas import DataFrame, Series\n",
    "from pandas.tseries.offsets import Day, Hour, Minute, MonthEnd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2000-01   -0.006518\n",
       "2000-02    0.017119\n",
       "2000-03   -0.113910\n",
       "2000-04    0.327016\n",
       "Freq: M, dtype: float64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rng = pd.date_range('1/1/2000', periods=100, freq='D')\n",
    "ts = Series(np.random.randn(len(rng)), index=rng)\n",
    "ts.resample('M', kind='period').mean() # 统计每月平均数，实际上是根据月份做了group。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# resample方法的参数\n",
    "# freq：\n",
    "# how已经被淘汰。原来的how='mean'写成resample(...).mean()。\n",
    "# axis：\n",
    "# fill_method已经被淘汰。原来的fill_method='...'写成resample(...).ffill()。方法为ffill或bfill，默认不插值。\n",
    "# close：      在降采样中，各时间段的哪一端是闭合的，'right'（默认）或'left'。\n",
    "# label：      在降采样中，如何设置聚合值的标签。'right'（默认）或'left'（面元的右/左边界）。\n",
    "#              例如，9:30到9:35之间的这5分钟会被标记成9:30或9:35。\n",
    "# loffset：    面元标签的时间校正值。比如'-1s'或Second(-1)用于将聚合标签调早1秒。\n",
    "# limit：      在向前或向后填充时，允许填充的最大时期数。默认None。\n",
    "# kind：       聚合到时期（'period'）或时间戳（'timestamp'）,默认聚合到时间序列的索引类型。\n",
    "# convertion： 当重采样时期时，将低频转换到高频采样所采用的约定（'start'或'end'），默认'end'。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 降采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2000-01-01 00:00:00     0\n",
       "2000-01-01 00:01:00     1\n",
       "2000-01-01 00:02:00     2\n",
       "2000-01-01 00:03:00     3\n",
       "2000-01-01 00:04:00     4\n",
       "2000-01-01 00:05:00     5\n",
       "2000-01-01 00:06:00     6\n",
       "2000-01-01 00:07:00     7\n",
       "2000-01-01 00:08:00     8\n",
       "2000-01-01 00:09:00     9\n",
       "2000-01-01 00:10:00    10\n",
       "2000-01-01 00:11:00    11\n",
       "Freq: T, dtype: int32"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rng = pd.date_range('1/1/2000', periods=12, freq='T') # T是按分钟\n",
    "ts = Series(np.arange(12), index=rng)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2000-01-01 00:00:00    10\n",
       "2000-01-01 00:05:00    35\n",
       "2000-01-01 00:10:00    21\n",
       "Freq: 5T, dtype: int32"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample(Minute(5)).sum() # 等价：ts.resample('5min').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1999-12-31 23:55:00     0\n",
       "2000-01-01 00:00:00    15\n",
       "2000-01-01 00:05:00    40\n",
       "2000-01-01 00:10:00    11\n",
       "Freq: 5T, dtype: int32"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('5min', closed='right').sum() # 比较一下left/right不同结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2000-01-01 00:05:00    10\n",
       "2000-01-01 00:10:00    35\n",
       "2000-01-01 00:15:00    21\n",
       "Freq: 5T, dtype: int32"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('5min', closed='left', label='right').sum() # 比较close/label不同组合的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1999-12-31 23:59:59    10\n",
       "2000-01-01 00:04:59    35\n",
       "2000-01-01 00:09:59    21\n",
       "Freq: 5T, dtype: int32"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('5min', loffset='-1s').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# OHLC重采样 Open/High/Low/Close"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-01 00:00:00</th>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-01 00:05:00</th>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-01 00:10:00</th>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     open  high  low  close\n",
       "2000-01-01 00:00:00     0     4    0      4\n",
       "2000-01-01 00:05:00     5     9    5      9\n",
       "2000-01-01 00:10:00    10    11   10     11"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('5min').ohlc() # 其实就是5分钟k线图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 通过groupby进行重采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    15\n",
       "2    45\n",
       "3    75\n",
       "4    95\n",
       "dtype: int32"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rng = pd.date_range('1/1/2000', periods=100, freq='D')\n",
    "ts = Series(np.arange(100), index=rng)\n",
    "ts.groupby(lambda x: x.month).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    47.5\n",
       "1    48.5\n",
       "2    49.5\n",
       "3    50.5\n",
       "4    51.5\n",
       "5    49.0\n",
       "6    50.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.groupby(lambda x: x.weekday).mean() # 0是周日"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 升采样和插值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Colorado</th>\n",
       "      <th>Texas</th>\n",
       "      <th>New York</th>\n",
       "      <th>Ohio</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-05</th>\n",
       "      <td>-2.366333</td>\n",
       "      <td>0.691709</td>\n",
       "      <td>-0.158442</td>\n",
       "      <td>2.562437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-12</th>\n",
       "      <td>-0.207933</td>\n",
       "      <td>-0.394861</td>\n",
       "      <td>0.264470</td>\n",
       "      <td>-1.361815</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Colorado     Texas  New York      Ohio\n",
       "2000-01-05 -2.366333  0.691709 -0.158442  2.562437\n",
       "2000-01-12 -0.207933 -0.394861  0.264470 -1.361815"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame = DataFrame(np.random.randn(2, 4),\n",
    "                  index=pd.date_range('1/1/2000', periods=2, freq='W-WED'),\n",
    "                  columns=['Colorado', 'Texas', 'New York', 'Ohio'])\n",
    "frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df_daily = frame.resample('D') # 注意警告信息，resample现在生成的是一个延迟计算对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Colorado', 2000-01-05   -2.366333\n",
      "2000-01-06         NaN\n",
      "2000-01-07         NaN\n",
      "2000-01-08         NaN\n",
      "2000-01-09         NaN\n",
      "2000-01-10         NaN\n",
      "2000-01-11         NaN\n",
      "2000-01-12   -0.207933\n",
      "Freq: D, Name: Colorado, dtype: float64)\n",
      "('Texas', 2000-01-05    0.691709\n",
      "2000-01-06         NaN\n",
      "2000-01-07         NaN\n",
      "2000-01-08         NaN\n",
      "2000-01-09         NaN\n",
      "2000-01-10         NaN\n",
      "2000-01-11         NaN\n",
      "2000-01-12   -0.394861\n",
      "Freq: D, Name: Texas, dtype: float64)\n",
      "('New York', 2000-01-05   -0.158442\n",
      "2000-01-06         NaN\n",
      "2000-01-07         NaN\n",
      "2000-01-08         NaN\n",
      "2000-01-09         NaN\n",
      "2000-01-10         NaN\n",
      "2000-01-11         NaN\n",
      "2000-01-12    0.264470\n",
      "Freq: D, Name: New York, dtype: float64)\n",
      "('Ohio', 2000-01-05    2.562437\n",
      "2000-01-06         NaN\n",
      "2000-01-07         NaN\n",
      "2000-01-08         NaN\n",
      "2000-01-09         NaN\n",
      "2000-01-10         NaN\n",
      "2000-01-11         NaN\n",
      "2000-01-12   -1.361815\n",
      "Freq: D, Name: Ohio, dtype: float64)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel\\__main__.py:1: FutureWarning: \n",
      ".resample() is now a deferred operation\n",
      "You called iteritems(...) on this deferred object which materialized it into a dataframe\n",
      "by implicitly taking the mean.  Use .resample(...).mean() instead\n",
      "  if __name__ == '__main__':\n"
     ]
    }
   ],
   "source": [
    "for i in df_daily.iteritems():\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Colorado</th>\n",
       "      <th>Texas</th>\n",
       "      <th>New York</th>\n",
       "      <th>Ohio</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-05</th>\n",
       "      <td>-2.366333</td>\n",
       "      <td>0.691709</td>\n",
       "      <td>-0.158442</td>\n",
       "      <td>2.562437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-06</th>\n",
       "      <td>-2.366333</td>\n",
       "      <td>0.691709</td>\n",
       "      <td>-0.158442</td>\n",
       "      <td>2.562437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-07</th>\n",
       "      <td>-2.366333</td>\n",
       "      <td>0.691709</td>\n",
       "      <td>-0.158442</td>\n",
       "      <td>2.562437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-08</th>\n",
       "      <td>-2.366333</td>\n",
       "      <td>0.691709</td>\n",
       "      <td>-0.158442</td>\n",
       "      <td>2.562437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-09</th>\n",
       "      <td>-2.366333</td>\n",
       "      <td>0.691709</td>\n",
       "      <td>-0.158442</td>\n",
       "      <td>2.562437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-10</th>\n",
       "      <td>-2.366333</td>\n",
       "      <td>0.691709</td>\n",
       "      <td>-0.158442</td>\n",
       "      <td>2.562437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-11</th>\n",
       "      <td>-2.366333</td>\n",
       "      <td>0.691709</td>\n",
       "      <td>-0.158442</td>\n",
       "      <td>2.562437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-12</th>\n",
       "      <td>-0.207933</td>\n",
       "      <td>-0.394861</td>\n",
       "      <td>0.264470</td>\n",
       "      <td>-1.361815</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Colorado     Texas  New York      Ohio\n",
       "2000-01-05 -2.366333  0.691709 -0.158442  2.562437\n",
       "2000-01-06 -2.366333  0.691709 -0.158442  2.562437\n",
       "2000-01-07 -2.366333  0.691709 -0.158442  2.562437\n",
       "2000-01-08 -2.366333  0.691709 -0.158442  2.562437\n",
       "2000-01-09 -2.366333  0.691709 -0.158442  2.562437\n",
       "2000-01-10 -2.366333  0.691709 -0.158442  2.562437\n",
       "2000-01-11 -2.366333  0.691709 -0.158442  2.562437\n",
       "2000-01-12 -0.207933 -0.394861  0.264470 -1.361815"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_daily = frame.resample('D', ).ffill() # ffill做填充，所以resample必须先展开计算。\n",
    "df_daily"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Colorado</th>\n",
       "      <th>Texas</th>\n",
       "      <th>New York</th>\n",
       "      <th>Ohio</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-05</th>\n",
       "      <td>-2.366333</td>\n",
       "      <td>0.691709</td>\n",
       "      <td>-0.158442</td>\n",
       "      <td>2.562437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-06</th>\n",
       "      <td>-2.366333</td>\n",
       "      <td>0.691709</td>\n",
       "      <td>-0.158442</td>\n",
       "      <td>2.562437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-07</th>\n",
       "      <td>-2.366333</td>\n",
       "      <td>0.691709</td>\n",
       "      <td>-0.158442</td>\n",
       "      <td>2.562437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-08</th>\n",
       "      <td>-2.366333</td>\n",
       "      <td>0.691709</td>\n",
       "      <td>-0.158442</td>\n",
       "      <td>2.562437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-09</th>\n",
       "      <td>-2.366333</td>\n",
       "      <td>0.691709</td>\n",
       "      <td>-0.158442</td>\n",
       "      <td>2.562437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-10</th>\n",
       "      <td>-2.366333</td>\n",
       "      <td>0.691709</td>\n",
       "      <td>-0.158442</td>\n",
       "      <td>2.562437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-11</th>\n",
       "      <td>-2.366333</td>\n",
       "      <td>0.691709</td>\n",
       "      <td>-0.158442</td>\n",
       "      <td>2.562437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-12</th>\n",
       "      <td>-0.207933</td>\n",
       "      <td>-0.394861</td>\n",
       "      <td>0.264470</td>\n",
       "      <td>-1.361815</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Colorado     Texas  New York      Ohio\n",
       "2000-01-05 -2.366333  0.691709 -0.158442  2.562437\n",
       "2000-01-06 -2.366333  0.691709 -0.158442  2.562437\n",
       "2000-01-07 -2.366333  0.691709 -0.158442  2.562437\n",
       "2000-01-08 -2.366333  0.691709 -0.158442  2.562437\n",
       "2000-01-09 -2.366333  0.691709 -0.158442  2.562437\n",
       "2000-01-10 -2.366333  0.691709 -0.158442  2.562437\n",
       "2000-01-11 -2.366333  0.691709 -0.158442  2.562437\n",
       "2000-01-12 -0.207933 -0.394861  0.264470 -1.361815"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_daily = frame.resample('D', limit=2).ffill() # 最多填充2次\n",
    "df_daily"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 通过时期进行重采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Colorado</th>\n",
       "      <th>Texas</th>\n",
       "      <th>New York</th>\n",
       "      <th>Ohio</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01</th>\n",
       "      <td>0.833519</td>\n",
       "      <td>0.378349</td>\n",
       "      <td>1.651265</td>\n",
       "      <td>0.218372</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-02</th>\n",
       "      <td>0.588505</td>\n",
       "      <td>0.358069</td>\n",
       "      <td>-1.187257</td>\n",
       "      <td>-0.352007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-03</th>\n",
       "      <td>-1.429854</td>\n",
       "      <td>-2.381661</td>\n",
       "      <td>-0.151033</td>\n",
       "      <td>-0.454657</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-04</th>\n",
       "      <td>-0.645234</td>\n",
       "      <td>-1.356728</td>\n",
       "      <td>-0.148877</td>\n",
       "      <td>-0.185001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-05</th>\n",
       "      <td>0.855779</td>\n",
       "      <td>1.392728</td>\n",
       "      <td>-0.656088</td>\n",
       "      <td>1.413772</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Colorado     Texas  New York      Ohio\n",
       "2000-01  0.833519  0.378349  1.651265  0.218372\n",
       "2000-02  0.588505  0.358069 -1.187257 -0.352007\n",
       "2000-03 -1.429854 -2.381661 -0.151033 -0.454657\n",
       "2000-04 -0.645234 -1.356728 -0.148877 -0.185001\n",
       "2000-05  0.855779  1.392728 -0.656088  1.413772"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame = DataFrame(np.random.randn(24, 4),\n",
    "                  index=pd.period_range('1-2000', '12-2001', freq='M'),\n",
    "                  columns=['Colorado', 'Texas', 'New York', 'Ohio'])\n",
    "frame.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Colorado</th>\n",
       "      <th>Texas</th>\n",
       "      <th>New York</th>\n",
       "      <th>Ohio</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000</th>\n",
       "      <td>-0.229285</td>\n",
       "      <td>-0.228343</td>\n",
       "      <td>-0.091366</td>\n",
       "      <td>0.303724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001</th>\n",
       "      <td>-0.029608</td>\n",
       "      <td>-0.584833</td>\n",
       "      <td>0.031789</td>\n",
       "      <td>0.027464</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Colorado     Texas  New York      Ohio\n",
       "2000 -0.229285 -0.228343 -0.091366  0.303724\n",
       "2001 -0.029608 -0.584833  0.031789  0.027464"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "annual_frame = frame.resample('A-DEC').mean()\n",
    "annual_frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Colorado</th>\n",
       "      <th>Texas</th>\n",
       "      <th>New York</th>\n",
       "      <th>Ohio</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000</th>\n",
       "      <td>-0.229285</td>\n",
       "      <td>-0.228343</td>\n",
       "      <td>-0.091366</td>\n",
       "      <td>0.303724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001</th>\n",
       "      <td>-0.029608</td>\n",
       "      <td>-0.584833</td>\n",
       "      <td>0.031789</td>\n",
       "      <td>0.027464</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Colorado     Texas  New York      Ohio\n",
       "2000 -0.229285 -0.228343 -0.091366  0.303724\n",
       "2001 -0.029608 -0.584833  0.031789  0.027464"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "annual_frame.resample('Q-DEC').ffill()\n",
    "annual_frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Colorado</th>\n",
       "      <th>Texas</th>\n",
       "      <th>New York</th>\n",
       "      <th>Ohio</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000</th>\n",
       "      <td>-0.229285</td>\n",
       "      <td>-0.228343</td>\n",
       "      <td>-0.091366</td>\n",
       "      <td>0.303724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001</th>\n",
       "      <td>-0.029608</td>\n",
       "      <td>-0.584833</td>\n",
       "      <td>0.031789</td>\n",
       "      <td>0.027464</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Colorado     Texas  New York      Ohio\n",
       "2000 -0.229285 -0.228343 -0.091366  0.303724\n",
       "2001 -0.029608 -0.584833  0.031789  0.027464"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "annual_frame.resample('Q-DEC', convention='start').ffill()\n",
    "annual_frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 一些讲解：\n",
    "# - 在降采样中，目标频率必须是源频率的子时期（subperiod）。降采样 <-> 高频到低频\n",
    "# - 在升采样中，目标频率必须是源频率的超时期（superperiod）。升采样 <-> 低频到高频\n",
    "# - 如果不满足这些条件，就会引发异常。这主要影响的是按季、年、周计算的频率。\n",
    "#   例如，由Q-MAR定义的时间区间只能升采样为A-MAR、A-JUN、A-SEP、A-DEC等。(有问题自己慢慢琢磨吧...)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Colorado</th>\n",
       "      <th>Texas</th>\n",
       "      <th>New York</th>\n",
       "      <th>Ohio</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000Q4</th>\n",
       "      <td>-0.229285</td>\n",
       "      <td>-0.228343</td>\n",
       "      <td>-0.091366</td>\n",
       "      <td>0.303724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001Q1</th>\n",
       "      <td>-0.229285</td>\n",
       "      <td>-0.228343</td>\n",
       "      <td>-0.091366</td>\n",
       "      <td>0.303724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001Q2</th>\n",
       "      <td>-0.229285</td>\n",
       "      <td>-0.228343</td>\n",
       "      <td>-0.091366</td>\n",
       "      <td>0.303724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001Q3</th>\n",
       "      <td>-0.229285</td>\n",
       "      <td>-0.228343</td>\n",
       "      <td>-0.091366</td>\n",
       "      <td>0.303724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001Q4</th>\n",
       "      <td>-0.029608</td>\n",
       "      <td>-0.584833</td>\n",
       "      <td>0.031789</td>\n",
       "      <td>0.027464</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002Q1</th>\n",
       "      <td>-0.029608</td>\n",
       "      <td>-0.584833</td>\n",
       "      <td>0.031789</td>\n",
       "      <td>0.027464</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002Q2</th>\n",
       "      <td>-0.029608</td>\n",
       "      <td>-0.584833</td>\n",
       "      <td>0.031789</td>\n",
       "      <td>0.027464</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002Q3</th>\n",
       "      <td>-0.029608</td>\n",
       "      <td>-0.584833</td>\n",
       "      <td>0.031789</td>\n",
       "      <td>0.027464</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Colorado     Texas  New York      Ohio\n",
       "2000Q4 -0.229285 -0.228343 -0.091366  0.303724\n",
       "2001Q1 -0.229285 -0.228343 -0.091366  0.303724\n",
       "2001Q2 -0.229285 -0.228343 -0.091366  0.303724\n",
       "2001Q3 -0.229285 -0.228343 -0.091366  0.303724\n",
       "2001Q4 -0.029608 -0.584833  0.031789  0.027464\n",
       "2002Q1 -0.029608 -0.584833  0.031789  0.027464\n",
       "2002Q2 -0.029608 -0.584833  0.031789  0.027464\n",
       "2002Q3 -0.029608 -0.584833  0.031789  0.027464"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "quater_frame = annual_frame.resample('Q-MAR').ffill()\n",
    "quater_frame"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
